
package com.welsy.authoring.vo
{
	import com.welsy.authoring.business.CourseQNames;
	import com.welsy.authoring.model.CourseModelLocator;
	import com.welsy.authoring.vo.course.pattern.CourseState;
	
	import mx.collections.ArrayCollection;
	import mx.collections.ICollectionView;	
		
	[Bindable]	
	public class CourseVO 
	{
		public function get identifier() : String
		{
			return String( id );
		}
	
		public function toString() : String
		{
		    var s : String = "CourseVO[id=";
		    s += id;
		    s += ", title=";
		    s += title;
		    s += ", parentId=";
		    s += parentCourseId;
		    s += " ]";
		    return s;
		}
			
		public var id : Number;
	
		public var title : String;
		
		public var titleUser : String;
		
		public var state:String = "";
				
		public var description:String = "";
		
		public var notes:String = "";
		
		public var webLink:String = "";
		
		public var authorId:String = "1";
		
		public var credits:String = "";
		
		public var parentCourseId:String = "";
		
		public var mapView:String = "true";
		
		public var navigationMap:String = "true";
		
		public var durationTime:String = "";
		
		public var target:String = "";
		
		public var isValid:Boolean = true;
			
		public function setProperties(item:XML):void
		{			
			this.id = item.child(CourseQNames.qnCourseID);
			this.title = item.child(CourseQNames.qnTitle).toString ();
			this.titleUser = item.child(CourseQNames.qnTitleUser).toString ();
			this.state = item.child(CourseQNames.qnElState).toString ();
            this.description = item.child(CourseQNames.qnElDescription).toString ();
            this.authorId = item.child(CourseQNames.qnElAuthorID).toString ();
            this.parentCourseId = item.child(CourseQNames.qnCourseParentCourseId).toString ();
            this.credits = item.child(CourseQNames.qnCourseCredits).toString ();
            this.webLink = item.child(CourseQNames.qnCourseWebLink).toString ();
            this.notes = item.child(CourseQNames.qnCourseNote).toString ();            	
            this.mapView = item.child(CourseQNames.qnCourseMapview).toString ();
            this.navigationMap = item.child(CourseQNames.qnCourseNavigationMap).toString ();
            this.durationTime = item.child(CourseQNames.qnDurationTime).toString ();
            this.target = item.child(CourseQNames.qnTarget).toString ();
		}
		
		public function MapEnable():Boolean 
		{			
      		if ((this.mapView == "1") || (this.mapView == "true"))
      			return true;
			else
				return false;
      	} 
      	
      	public function NavigationMapEnable():Boolean 
		{			
      		if ((this.navigationMap == "1") || (this.navigationMap == "true"))
      			return true;
			else
				return false;
      	} 
      
		public function enableItemTest():Boolean
		{
			if ((this.state == CourseState.publish) || (this.parentCourseId != "0"))
				return false
		    else
		    	return true
		}	
	
		public function getParent():CourseVO
		{
			var Icourses:ICollectionView = CourseModelLocator.getInstance().courses
			var myCourse:CourseVO
			var returnCourse:CourseVO

			if ((Icourses != null) && (this.parentCourseId != "0")) {
				var acCourses:ArrayCollection = ArrayCollection(Icourses)

				for each(myCourse in acCourses){					
					if (Number(this.parentCourseId) == myCourse.id) {
						returnCourse = myCourse
						break;
					}
				}
			}

			return returnCourse
		}	
		
		public function setCourses():void{
			var Icourses:ICollectionView = CourseModelLocator.getInstance().courses
			var myCourse:CourseVO
			
			if (Icourses != null) {
				var acCourses:ArrayCollection = ArrayCollection(Icourses)

				for (var i:int=0; i<acCourses.length; i++){
					myCourse = acCourses[i]						
					if (this.id == myCourse.id) {
						acCourses[i] = this
						break;
					}
				}
			}
		}
	}

}